#include <assert.h>
#include <stdio.h>
#include <curl/curl.h>

size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) {
	FILE *file = (FILE *) userp;
	
	assert (userp != NULL);
	
	return fwrite( buffer,  size,  nmemb, file);
}

int main (int argc, char **argv) {
	CURL *curl = NULL;
	CURLcode res;
	FILE *picture = NULL;
	
	if (argc != 2) {
		printf ("Give me the url of a jpg!.\n");
		return -1;
	}

	/* Init CURL */
	curl = curl_easy_init ();
	assert (curl != NULL);
	
	picture = fopen ("picture.jpg", "w");
	
	/* Config */
	curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); 
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)picture); 

	res = curl_easy_perform (curl);
	
	/* Free the data */
	curl_easy_cleanup (curl);
	curl = NULL;
	
	return 0;
}


